<?php
$conn = mysql_connect("localhost","root","") or die("sorry");
$db = mysql_select_db("sample") or  die("die");
if($_POST['action']=="insert"){
$margin = $_POST['lefts'];
$q = mysql_query("INSERT into margin values('','$margin',NOW())");
$id = mysql_insert_id();
mysql_query("delete from margin where id < $id");
}elseif($_POST['action']=="select"){
    $q = mysql_query("Select * from margin order by datetime asc");
    while($activeMargin = mysql_fetch_array($q)){
    $margin =  $activeMargin['margin'];
    }
    echo $margin;
}
?>